<template>
  <div class="login-panel">
    <!-- 顶部标题 -->
    <h1 class="title">后台管理系统</h1>
    <!-- 账号密码区域 -->
    <div class="tabs">
      <el-tabs type="border-card" stretch v-model="activeName">
        <!-- 账号登陆界面 -->
        <el-tab-pane label="账号登陆" name="account">
          <template #label>
            <div class="label">
              <!-- 字体图标，plus每个页面必须要单独导入图标 -->
              <el-icon><User /></el-icon>
              <span class="text">帐号登陆</span>
            </div>
          </template>
          <PaneAccount ref="accountRef" />
        </el-tab-pane>
        <!-- 手机登录界面 -->
        <el-tab-pane label="手机登录" name="phone">
          <template #label>
            <div class="label">
              <el-icon><Message /></el-icon>
              <span class="text">手机登陆</span>
            </div>
          </template>
          <PanePhone />
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 底部区域 -->
    <div class="control-account">
      <el-checkbox v-model="isRemPwd" label="记住密码" size="large" />
      <el-link type="primary" href="#">忘记密码</el-link>
    </div>

    <el-button class="login-btn" type="primary" size="large" @click="handleLoginBtnClick"
      >立即登录</el-button
    >
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
// 引入账号和密码的组件
import PaneAccount from './pane-account.vue'
import PanePhone from './pane-phone.vue'
import { localcache } from '@/utils/cache'

// 判断是否保存密码  设置布尔值，就是说如果是undefined时，也会转成false
const isRemPwd = ref<boolean>(localcache.getCache('isRemPwd') ?? false)
// 实时监听属性的这个值
watch(isRemPwd, (newVal) => {
  localcache.setCache('isRemPwd', newVal)
})
// 获取子组件
const accountRef = ref<InstanceType<typeof PaneAccount>>()
function handleLoginBtnClick() {
  if (activeName.value === 'account') {
    // console.log('用户在进行账号登陆')

    accountRef.value?.loginAction(isRemPwd.value)
    // 记住帐号和密码
    if (isRemPwd.value) {
      console.log('记住账号和密码')
    }
  } else {
    console.log('用户进行手机登录')
  }
}
// 判断处于哪种登录状态
const activeName = ref('account')
</script>

<style lang="less" scoped>
.login-panel {
  width: 330px;
  margin-bottom: 150px;

  .title {
    text-align: center;
    margin-bottom: 15px;
    font-size: 18px;
    font-weight: bold;
  }

  .label {
    display: flex;
    align-items: center;
    justify-content: center;

    .text {
      margin-left: 8px;
    }
  }

  .control-account {
    margin-top: 12px;
    display: flex;

    justify-content: space-between;
  }

  .login-btn {
    margin-top: 10px;
    width: 100%;
  }
}
</style>
